<?php

/**
 * CourrierElectronique form base class.
 *
 * @method CourrierElectronique getObject() Returns the current form's model object
 *
 * @package    tfe
 * @subpackage form
 * @author     3emeIG
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseCourrierElectroniqueForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                             => new sfWidgetFormInputHidden(),
      'piece_jointe'                   => new sfWidgetFormTextarea(),
      'objet'                          => new sfWidgetFormInputText(),
      'message'                        => new sfWidgetFormTextarea(),
      'created_at'                     => new sfWidgetFormDateTime(),
      'updated_at'                     => new sfWidgetFormDateTime(),
      'deleted_at'                     => new sfWidgetFormDateTime(),
      'contacts_destinataires_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Contact')),
      'groupes_destinataires_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Groupe')),
      'newsletters_destinataires_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Newsletter')),
      'ateliers_destinataires_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Atelier')),
      'stages_destinataires_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Stage')),
      'abonnements_destinataires_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Abonnement')),
    ));

    $this->setValidators(array(
      'id'                             => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'piece_jointe'                   => new sfValidatorString(array('required' => false)),
      'objet'                          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'message'                        => new sfValidatorString(),
      'created_at'                     => new sfValidatorDateTime(),
      'updated_at'                     => new sfValidatorDateTime(),
      'deleted_at'                     => new sfValidatorDateTime(array('required' => false)),
      'contacts_destinataires_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Contact', 'required' => false)),
      'groupes_destinataires_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Groupe', 'required' => false)),
      'newsletters_destinataires_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Newsletter', 'required' => false)),
      'ateliers_destinataires_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Atelier', 'required' => false)),
      'stages_destinataires_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Stage', 'required' => false)),
      'abonnements_destinataires_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Abonnement', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('courrier_electronique[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'CourrierElectronique';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['contacts_destinataires_list']))
    {
      $this->setDefault('contacts_destinataires_list', $this->object->ContactsDestinataires->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['groupes_destinataires_list']))
    {
      $this->setDefault('groupes_destinataires_list', $this->object->GroupesDestinataires->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['newsletters_destinataires_list']))
    {
      $this->setDefault('newsletters_destinataires_list', $this->object->NewslettersDestinataires->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['ateliers_destinataires_list']))
    {
      $this->setDefault('ateliers_destinataires_list', $this->object->AteliersDestinataires->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['stages_destinataires_list']))
    {
      $this->setDefault('stages_destinataires_list', $this->object->StagesDestinataires->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['abonnements_destinataires_list']))
    {
      $this->setDefault('abonnements_destinataires_list', $this->object->AbonnementsDestinataires->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveContactsDestinatairesList($con);
    $this->saveGroupesDestinatairesList($con);
    $this->saveNewslettersDestinatairesList($con);
    $this->saveAteliersDestinatairesList($con);
    $this->saveStagesDestinatairesList($con);
    $this->saveAbonnementsDestinatairesList($con);

    parent::doSave($con);
  }

  public function saveContactsDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['contacts_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->ContactsDestinataires->getPrimaryKeys();
    $values = $this->getValue('contacts_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('ContactsDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('ContactsDestinataires', array_values($link));
    }
  }

  public function saveGroupesDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['groupes_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->GroupesDestinataires->getPrimaryKeys();
    $values = $this->getValue('groupes_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('GroupesDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('GroupesDestinataires', array_values($link));
    }
  }

  public function saveNewslettersDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['newsletters_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->NewslettersDestinataires->getPrimaryKeys();
    $values = $this->getValue('newsletters_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('NewslettersDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('NewslettersDestinataires', array_values($link));
    }
  }

  public function saveAteliersDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['ateliers_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->AteliersDestinataires->getPrimaryKeys();
    $values = $this->getValue('ateliers_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('AteliersDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('AteliersDestinataires', array_values($link));
    }
  }

  public function saveStagesDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['stages_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->StagesDestinataires->getPrimaryKeys();
    $values = $this->getValue('stages_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('StagesDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('StagesDestinataires', array_values($link));
    }
  }

  public function saveAbonnementsDestinatairesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['abonnements_destinataires_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->AbonnementsDestinataires->getPrimaryKeys();
    $values = $this->getValue('abonnements_destinataires_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('AbonnementsDestinataires', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('AbonnementsDestinataires', array_values($link));
    }
  }

}
